#include <trax.hpp>
#include "atBVHNode.h"
#include "Ray.h"
#include "HitRecord.h"

namespace AtomTrace{
    atBVHNode::atBVHNode():
    		start_addr(GetBVH())
    {
        numTri = -1;
        //this->LoadFromGM(0);
        start_addr = GetBVH();
    }

    atBVHNode::atBVHNode(int nodeID):
    	    start_addr(GetBVH())
    {
        numTri = -1;
        this->LoadFromGM(nodeID);
    }

    void atBVHNode::LoadFromGM(int nodeID)
    {
        int addr = start_addr + nodeID*8; // each node is 8 words
        mBox.pmin.LoadFromGM(addr);
        mBox.pmax.LoadFromGM(addr+3);

        numTri = loadi(addr+6);
        childID = loadi(addr+7);
        
    }

    bool atBVHNode::Intersect(const Ray & ray, float & hit)
    {
        return this->mBox.Intersect(ray, hit);
    }

} // namespace AtomTrace
